// =============================================================
// CSpace test
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Sep. 4, 2007
// =============================================================
#include <iostream>

#include "CSpace.h"
#include "CMessage.h"

int main(int argc, char **argv)
{
	CSpace space("simple test");
	CMessage msg1, msg2;

	msg1.set("woorung nara");
	cout << "put message : " << msg1.get() << endl;
	cout << "current size = " << space.write(&msg1) << endl;

vector< CSmartTemplate<CObject> > all;
all = space.read(NULL, space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	msg2.set("hahaha babo");
	cout << "put message : " << msg2.get() << endl;
	cout << "current size = " << space.write(&msg2) << endl;

all.clear();
all = space.read(NULL, space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << "take message :";
	CMessage *res1 = NULL;
	res1 = static_cast<CMessage *>(space.take(res1));
	cout << res1->get() << endl;
	delete res1;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << "read message :";
	CMessage *res2 = NULL;
	res2 = static_cast<CMessage *>(space.read(res2));
	cout << res2->get() << endl;
	delete res2;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << "1st current size = " << space.size() << endl;
	cout << "---------------------------------------------" << endl << endl << endl;


	cout << "put message : " << msg1.get() << endl;
	cout << "current size = " << space.write(&msg1) << endl;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << "put message : " << msg2.get() << endl;
	cout << "current size = " << space.write(&msg2) << endl;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << "read message :";
	res1 = static_cast<CMessage *>(space.read(res1));
	cout << res1->get() << endl;
	delete res1;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << "take message :";
	res2 = static_cast<CMessage *>(space.take(res2));
	cout << res2->get() << endl;
	delete res2;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << "take message :";
	res2 = static_cast<CMessage *>(space.take(res2));
	cout << res2->get() << endl;
	delete res2;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;
	
	cout << "2st current size = " << space.size() << endl;
	res2 = static_cast<CMessage *>(space.take(res2));
	cout << "the rest of message : " << res2->get() << endl;
	cout << "last current size = " << space.size() << endl;
	delete res2;

all.clear();
all = space.readIfExists(NULL, space.size(), all);
//all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	return 1;
}
